package free.order.security.action;

import java.util.List;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.springframework.beans.factory.annotation.Required;

import free.order.common.action.BaseAction;
import free.order.security.entity.Resource;
import free.order.security.entity.Role;
import free.order.security.service.SecurityService;

/**
 * 角色管理Action.
 * 
 * @see CRUDActionSupport
 * @author stan
 */
public class RoleAction extends BaseAction {

	private static final long serialVersionUID = 8517636900960039101L;

	private SecurityService securityServ;

	private List<Role> allRoles;

	private Role entity;

	private Long id;

	private List<Long> checkedAuthIds;

	private List<Resource> allAuths;

	public Role getModel() {
		return entity;
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public List<Resource> getAllAuths() {
		return allAuths;
	}

	public List<Long> getCheckedAuthIds() {
		return checkedAuthIds;
	}

	public void setCheckedAuthIds(List<Long> checkedAuthIds) {
		this.checkedAuthIds = checkedAuthIds;
	}

	public void setId(Long id) {
		this.id = id;
	}

	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = securityServ.getRole(id);
		} else {
			entity = new Role();
		}
	}

	public String list() throws Exception {
//		allRoles = manager.getAllRoles();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
//		allAuths = manager.getAllAuths();
		checkedAuthIds = entity.getAuthIds();
		return INPUT;
	}

	public String save() throws Exception {
		//根据页面上的checkbox 整合entity的auth Set.
//		CollectionUtils.mergeByCheckedIds(entity.getAuths(), checkedAuthIds, Resource.class);
//		securityServ.saveRole(entity);
//		addActionMessage("保存角色成功");
		return "RELOAD";
	}

	public String delete() throws Exception {
//		manager.deleteRole(id);
		addActionMessage("删除角色成功");
		return "RELOAD";
	}

	public void setSecurityServ(SecurityService securityServ) {
		this.securityServ = securityServ;
	}
	
	public SecurityService getSecurityServ(){
		return securityServ;
	}
}